package com.leizi.igym.controller.admin;

import com.leizi.igym.dto.DoughnutStatisticsDTO;
import com.leizi.igym.dto.LineStatisticsDTO;
import com.leizi.igym.dto.PanelStatisticsDTO;
import com.leizi.igym.enums.DoughnutStatisticsTypeEnum;
import com.leizi.igym.enums.LineStatisticsDateEnum;
import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import com.leizi.igym.resp.Result;
import com.leizi.igym.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-03-19  22:04
 * @Description:
 */


@RestController("adminWorkSpaceController")
@RequestMapping("/admin/workspace")
@Tag(name = "管理端-工作台相关接口")
@Slf4j
public class WorkSpaceController {

    @Resource
    private MemberService memberService;
    @Resource
    private UserService userService;

    @Resource
    private TrainerService trainerService;

    @Resource
    private CourseService courseService;

    @Resource
    private EmployeeService employeeService;

    @Operation(summary = "管理端——获取会员部分信息用于饼状图统计")
    @GetMapping("doughnut")
    public Result getDoughnutStatistics(@RequestParam("type") String type) {
        if (StringUtils.isBlank(type) || Objects.isNull(DoughnutStatisticsTypeEnum.getTypeValue(type))) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR);
        }
        List<DoughnutStatisticsDTO> list = memberService.getDoughnutStatisticsByType(type);
        return Result.ok(list);
    }

    @Operation(summary = "管理端——获取会员数据用于折线图统计")
    @GetMapping("memberLine")
    public Result getLineStatistics(@RequestParam("date") Integer date, @RequestParam("type") String type) {
        if (Objects.isNull(date)
                || Objects.isNull(LineStatisticsDateEnum.getDateValue(date))
                || StringUtils.isBlank(type)
                || !List.of("member", "user").contains(type)) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR);
        }
        LineStatisticsDTO res = null;
        if (type.equals("member")) {
            res = memberService.getLineStatisticsByDate(date);
        } else {
            res = userService.getLineStatisticsByDate(date);
        }
        return Result.ok(res);
    }

    @Operation(summary = "管理端——获取会员数据用于折线图统计")
    @GetMapping("panel")
    public Result getPanelStatistics() {
        return Result.ok(PanelStatisticsDTO.builder()
                .empCount(employeeService.count())
                .courseCount(courseService.count())
                .trainerCount(trainerService.count())
                .memberCount(memberService.count())
                .build());
    }

}
